﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI;
using CoreLab.Oracle; //using Devart.Data.Oracle; //using Oracle.DataAccess.Client;

using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;
using LMD.Lib.Oracle;

public partial class UFTConfirm : ICPRelationshipPage
{
    public UFTData m_uftData = new UFTData();
    public int m_iEventId = 0;
    public bool bCheckClose = false;
    public bool bSaved = false;
        
    #region Properties
    public String m_sxEventId
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["eid"]))
            {
                return Request.QueryString["eid"];
            }
            else
            {
                return String.Empty;
            }
        }
    }
    #endregion Properties

    #region Event Handlers
    protected override void Page_Load(object sender, EventArgs e)
    {
        m_lsPermittedRoles.Add(ICPUserRole.CAR);
        base.Page_Load(sender, e);
        Page.Title = "ICP Lintasarta: Relationship, Confirmation";
        m_iEventId = Context.ReadInt("eid");
        if (!IsPostBack && !IsCallback)
        {
            InitUFTData();
        }
        bSaved = DBQuery.CustomerTable.checkSaveConfirm(m_iEventId);
        InitGuestInvitation();
    }
    
    protected void dgCustomerList_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        if (e.Parameters.ToUpper().StartsWith("SAVE"))
        {
            SaveKehadiran();
        }
        else if (e.Parameters.ToUpper().StartsWith("REFRESH"))
        {
            InitGuestInvitation();
        }
    }
    protected void cbkConfirm_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        e.Result = HandleConfirm(e.Parameter);
    }
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    private void InitUFTData()
    {
        if (m_iEventId > 0)
        {
            DataTable dtEventTable = DBQuery.EventTable.GetEventDataByID(m_iEventId);
            m_uftData.sxCode = dtEventTable.Rows[0]["CODE"].ToString();
            m_uftData.dtmEventDate = Convert.ToDateTime(dtEventTable.Rows[0]["EVENT_DATE"]);
            m_uftData.iEventId = Convert.ToInt32(dtEventTable.Rows[0]["P_EVENT_ID"]);
            if (dtEventTable.Rows[0]["INVITATION_ATTACHMENT_ID"] == DBNull.Value)
            {
                m_uftData.iInvitationAttachmentId = 0;
            }
            else
            {
                m_uftData.iInvitationAttachmentId = Convert.ToInt32(dtEventTable.Rows[0]["INVITATION_ATTACHMENT_ID"]);
            }
            if (dtEventTable.Rows[0]["EVENT_ATTACHMENT_ID"] == DBNull.Value)
            {
                m_uftData.iEventAttachmentId = 0;
            }
            else
            {
                m_uftData.iEventAttachmentId = Convert.ToInt32(dtEventTable.Rows[0]["EVENT_ATTACHMENT_ID"]);
            }
            if (dtEventTable.Rows[0]["PRESENTATION_ID"] == DBNull.Value)
            {
                m_uftData.iPresentationId = 0;
            }
            else
            {
                m_uftData.iPresentationId = Convert.ToInt32(dtEventTable.Rows[0]["PRESENTATION_ID"]);
            }
            if (dtEventTable.Rows[0]["LOCATION"] != DBNull.Value)
            {
                m_uftData.sxLocation = dtEventTable.Rows[0]["LOCATION"].ToString();
            }
            if (dtEventTable.Rows[0]["START_TIME"] != DBNull.Value)
            {
                m_uftData.dtmStartTime = Convert.ToDateTime(dtEventTable.Rows[0]["START_TIME"]);
            }
            if (dtEventTable.Rows[0]["END_TIME"] != DBNull.Value)
            {
                m_uftData.dtmEndTime = Convert.ToDateTime(dtEventTable.Rows[0]["END_TIME"]);
            }
            m_uftData.BuildFieldSet_UFTData();
        }
    }

    private void InitGuestInvitation()
    {
        String sxCARID = ICPUser.ID;
        //m_iEventId = Convert.ToInt32(Session[KSession.UFT_EVENTID]);
        DataTable dtSelected = DBQuery.CustomerTable.GetCustomerConfirmation(m_iEventId, sxCARID);

        dgCustomerList.DataSource = dtSelected;
        //disini buat nampilin yg selection
        for (int i = 0; i < dtSelected.Rows.Count; i++)
        {
            dgCustomerList.Selection.SetSelectionByKey(
                dtSelected.Rows[i]["T_RELATION_INVITATION_ID"],
                dtSelected.Rows[i]["STATUS_CONFIRM"].ToString() == "HADIR");
        }
        //
        dgCustomerList.DataBind();
        dgCustomerList.Columns["CUSTOMER_ID"].Visible = false;
        dgCustomerList.Columns["COMPANY_NAME"].Visible = true;
        dgCustomerList.Columns["T_RELATION_INVITATION_ID"].Visible = false;
    }

    private String HandleConfirm(String sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);

        String sxOutput = String.Empty;
        int iInvID = -1;
        switch (sxSplitted[0].ToUpper())
        {
            case "REASON":
                iInvID = Convert.ToInt32(sxSplitted[1]);
                sxOutput = UFTManager.BuildEditor_Reason(iInvID.ToString());
                break;
            case "SUBMIT":
                iInvID = Convert.ToInt32(sxSplitted[1]);
                int iReasonId = Convert.ToInt32(sxSplitted[2]);
                DBQuery.CustomerTable.SaveReasonNotConfirm(iInvID, iReasonId);
                //DBQuery.CustomerVisitTable.EditVisitDatetime(iCVID, dtmVisitDatetime);
                //InitVisitList(m_sxMode);
                break;
			case "CONFIRM":
                DBQuery.CustomerTable.SubmitConfirm(m_iEventId);
                break;
        }
        return sxOutput;
    }

    private void SaveKehadiran()
    {
        DataTable dtOld = DBQuery.CustomerTable.GetCustomerConfirmation(m_iEventId, ICPUser.ID);
        DataTable dtUpdate = (DataTable)dgCustomerList.DataSource;

        DBLib dbl = new DBLib();
        dbl.BeginTransaction();
        try
        {
            for (int i = 0; i < dtOld.Rows.Count; i++)
            {
                bool IsOldSelected = dtOld.Rows[i]["STATUS_CONFIRM"].ToString() == "HADIR";
                int iInvId = Convert.ToInt32(dtOld.Rows[i]["T_RELATION_INVITATION_ID"]);
                int iEventId = Convert.ToInt32(dtOld.Rows[i]["T_RELATION_EVENT_ID"]);
                bool IsNewSelected = dgCustomerList.Selection.IsRowSelectedByKey(dtOld.Rows[i]["T_RELATION_INVITATION_ID"]);

                if (IsOldSelected && !IsNewSelected)
                {
                    DBQuery.CustomerTable.UpdateStatusConfirm(dbl, iInvId, iEventId, false);
                }
                else if (!IsOldSelected && IsNewSelected)
                {
                    DBQuery.CustomerTable.UpdateStatusConfirm(dbl, iInvId, iEventId, true);
                }
            }
            // COMMIT 
            dbl.EndTransaction(true);
            //bSaved = true;
            InitGuestInvitation();
            //dgCustomerList.Columns["STATUS_CONFIRM"].Visible = true;
        }
        catch (Exception ex)
        {
            // ROLLBACK
            dbl.EndTransaction(false);
            throw ex;
        }

    }
    
    #endregion Custom Methods
}
